package top.huxy.web.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.huxy.web.config.argumentResolver.UniversalArgumentResolver;
import top.huxy.web.config.interceptor.CrosInterception;
import top.huxy.web.config.messageConverter.ResultMessageConverter;

import java.util.List;

/**
 * @author aproz
 * @Date 2022/2/24
 */
@Configuration
@Import({
        CrosInterception.class,
        UniversalArgumentResolver.class,
        ResultMessageConverter.class
})
@Slf4j
public class HuxyWebAppAutoConfig {

    @Bean("webAppCommonMvcConfigure")
    public WebMvcConfigure webMvcConfigure() {
        return new WebMvcConfigure();
    }


    public static class WebMvcConfigure implements WebMvcConfigurer {


        @Autowired(required = false)
        CrosInterception crosInterception;

        @Autowired(required = false)
        UniversalArgumentResolver universalArgumentResolver;

        @Autowired(required = false)
        ResultMessageConverter resultMessageConverter;

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            if(crosInterception!=null){
                registry.addInterceptor(crosInterception);
            }
        }

        @Override
        public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
            if(universalArgumentResolver!=null){
                resolvers.add(universalArgumentResolver);
            }
        }

        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            if(resultMessageConverter!=null){
                converters.add(0,resultMessageConverter);
            }
        }
    }
}
